<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">

      <el-form-item label="车牌号" prop="plateNum">
        <el-input
          v-model="queryParams.plateNum"
          placeholder="请输入车牌号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="品牌型号" prop="model">
        <el-input
          v-model="queryParams.model"
          placeholder="请输入品牌型号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="所有人" prop="owner">
        <el-input
          v-model="queryParams.owner"
          placeholder="请输入所有人"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="车辆识别代码" prop="vin">
        <el-input
          v-model="queryParams.vin"
          placeholder="请输入车辆识别代码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="住址" prop="addr">
        <el-input
          v-model="queryParams.addr"
          placeholder="请输入住址"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="使用性质" prop="useCharacter">
        <el-input
          v-model="queryParams.useCharacter"
          placeholder="请输入使用性质"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="发动机号码" prop="engineNum">
        <el-input
          v-model="queryParams.engineNum"
          placeholder="请输入发动机号码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="发证机关" prop="officialSeal">
        <el-input
          v-model="queryParams.officialSeal"
          placeholder="请输入发证机关"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="发证日期" prop="issueDate">
        <el-date-picker clearable
          v-model="queryParams.issueDate"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择发证日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="注册日期" prop="registerDate">
        <el-date-picker clearable
          v-model="queryParams.registerDate"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择注册日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="档案编号" prop="fileNumber">
        <el-input
          v-model="queryParams.fileNumber"
          placeholder="请输入档案编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="总质量" prop="totalQuality">
        <el-input
          v-model="queryParams.totalQuality"
          placeholder="请输入总质量"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="准牵引总质量" prop="leadQuality">
        <el-input
          v-model="queryParams.leadQuality"
          placeholder="请输入准牵引总质量"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="核定载质量" prop="loadQuality">
        <el-input
          v-model="queryParams.loadQuality"
          placeholder="请输入核定载质量"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="外廓尺寸" prop="overallSize">
        <el-input
          v-model="queryParams.overallSize"
          placeholder="请输入外廓尺寸"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="核定载人数" prop="passengersNum">
        <el-input
          v-model="queryParams.passengersNum"
          placeholder="请输入核定载人数"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="整备质量" prop="prepareQuality">
        <el-input
          v-model="queryParams.prepareQuality"
          placeholder="请输入整备质量"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="档案编号" prop="record">
        <el-input
          v-model="queryParams.record"
          placeholder="请输入档案编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="备注" prop="remarks">
        <el-input
          v-model="queryParams.remarks"
          placeholder="请输入备注"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="轴数" prop="axleCount">
        <el-input
          v-model="queryParams.axleCount"
          placeholder="请输入轴数"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="核载质量" prop="loadCapacity">
        <el-input
          v-model="queryParams.loadCapacity"
          placeholder="请输入核载质量"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="是否删除" prop="isDelete">
        <el-input
          v-model="queryParams.isDelete"
          placeholder="请输入是否删除"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['consumer:car:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['consumer:car:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['consumer:car:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['consumer:car:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="carList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="车辆id编号" align="center" prop="carId" />
      <el-table-column label="所属公司" align="center" prop="createBy" />
      <el-table-column label="车牌号" align="center" prop="plateNum" />
      <el-table-column label="品牌型号" align="center" prop="model" />
      <el-table-column label="车辆照片" align="center" prop="picture" width="100">
        <template slot-scope="scope">
          <image-preview :src="scope.row.picture" :width="50" :height="50"/>
        </template>
      </el-table-column>
      <el-table-column label="行驶证正面" align="center" prop="Permit" width="100">
        <template slot-scope="scope">
          <image-preview :src="scope.row.Permit" :width="50" :height="50"/>
        </template>
      </el-table-column>
      <el-table-column label="行驶证反面" align="center" prop="permitB" width="100">
        <template slot-scope="scope">
          <image-preview :src="scope.row.permitB" :width="50" :height="50"/>
        </template>
      </el-table-column>
      <el-table-column label="所有人" align="center" prop="owner" />
      <el-table-column label="车辆识别代码" align="center" prop="vin" />
      <el-table-column label="住址" align="center" prop="addr" />
      <el-table-column label="车辆类型" align="center" prop="vehicleType" />
      <el-table-column label="使用性质" align="center" prop="useCharacter" />
      <el-table-column label="发动机号码" align="center" prop="engineNum" />
      <el-table-column label="发证机关" align="center" prop="officialSeal" />
      <el-table-column label="发证日期" align="center" prop="issueDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.issueDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="注册日期" align="center" prop="registerDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.registerDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="档案编号" align="center" prop="fileNumber" />
      <el-table-column label="总质量" align="center" prop="totalQuality" />
      <el-table-column label="准牵引总质量" align="center" prop="leadQuality" />
      <el-table-column label="核定载质量" align="center" prop="loadQuality" />
      <el-table-column label="外廓尺寸" align="center" prop="overallSize" />
      <el-table-column label="核定载人数" align="center" prop="passengersNum" />
      <el-table-column label="整备质量" align="center" prop="prepareQuality" />
      <el-table-column label="档案编号" align="center" prop="record" />
      <el-table-column label="备注" align="center" prop="remarks" />
      <el-table-column label="轴数" align="center" prop="axleCount" />
      <el-table-column label="核载质量" align="center" prop="loadCapacity" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['consumer:car:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['consumer:car:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改车辆信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="车牌号" prop="plateNum">
          <el-input v-model="form.plateNum" placeholder="请输入车牌号" />
        </el-form-item>
        <el-form-item label="品牌型号" prop="model">
          <el-input v-model="form.model" placeholder="请输入品牌型号" />
        </el-form-item>
        <el-form-item label="车辆照片" prop="picture">
          <image-upload v-model="form.picture"/>
        </el-form-item>
        <el-form-item label="行驶证正面" prop="Permit">
          <image-upload v-model="form.Permit"/>
        </el-form-item>
        <el-form-item label="行驶证反面" prop="permitB">
          <image-upload v-model="form.permitB"/>
        </el-form-item>
        <el-form-item label="所有人" prop="owner">
          <el-input v-model="form.owner" placeholder="请输入所有人" />
        </el-form-item>
        <el-form-item label="车辆识别代码" prop="vin">
          <el-input v-model="form.vin" placeholder="请输入车辆识别代码" />
        </el-form-item>
        <el-form-item label="住址" prop="addr">
          <el-input v-model="form.addr" placeholder="请输入住址" />
        </el-form-item>
        <el-form-item label="使用性质" prop="useCharacter">
          <el-input v-model="form.useCharacter" placeholder="请输入使用性质" />
        </el-form-item>
        <el-form-item label="发动机号码" prop="engineNum">
          <el-input v-model="form.engineNum" placeholder="请输入发动机号码" />
        </el-form-item>
        <el-form-item label="发证机关" prop="officialSeal">
          <el-input v-model="form.officialSeal" placeholder="请输入发证机关" />
        </el-form-item>
        <el-form-item label="发证日期" prop="issueDate">
          <el-date-picker clearable
            v-model="form.issueDate"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择发证日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="注册日期" prop="registerDate">
          <el-date-picker clearable
            v-model="form.registerDate"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择注册日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="档案编号" prop="fileNumber">
          <el-input v-model="form.fileNumber" placeholder="请输入档案编号" />
        </el-form-item>
        <el-form-item label="总质量" prop="totalQuality">
          <el-input v-model="form.totalQuality" placeholder="请输入总质量" />
        </el-form-item>
        <el-form-item label="准牵引总质量" prop="leadQuality">
          <el-input v-model="form.leadQuality" placeholder="请输入准牵引总质量" />
        </el-form-item>
        <el-form-item label="核定载质量" prop="loadQuality">
          <el-input v-model="form.loadQuality" placeholder="请输入核定载质量" />
        </el-form-item>
        <el-form-item label="外廓尺寸" prop="overallSize">
          <el-input v-model="form.overallSize" placeholder="请输入外廓尺寸" />
        </el-form-item>
        <el-form-item label="核定载人数" prop="passengersNum">
          <el-input v-model="form.passengersNum" placeholder="请输入核定载人数" />
        </el-form-item>
        <el-form-item label="整备质量" prop="prepareQuality">
          <el-input v-model="form.prepareQuality" placeholder="请输入整备质量" />
        </el-form-item>
        <el-form-item label="档案编号" prop="record">
          <el-input v-model="form.record" placeholder="请输入档案编号" />
        </el-form-item>
        <el-form-item label="备注" prop="remarks">
          <el-input v-model="form.remarks" placeholder="请输入备注" />
        </el-form-item>
        <el-form-item label="轴数" prop="axleCount">
          <el-input v-model="form.axleCount" placeholder="请输入轴数" />
        </el-form-item>
        <el-form-item label="核载质量" prop="loadCapacity">
          <el-input v-model="form.loadCapacity" placeholder="请输入核载质量" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listCar, getCar, delCar, addCar, updateCar } from "@/api/consumer/car";

export default {
  name: "Car",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 车辆信息表格数据
      carList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        companyId: null,
        plateNum: null,
        model: null,
        picture: null,
        Permit: null,
        permitB: null,
        owner: null,
        vin: null,
        addr: null,
        vehicleType: null,
        useCharacter: null,
        engineNum: null,
        officialSeal: null,
        issueDate: null,
        registerDate: null,
        fileNumber: null,
        totalQuality: null,
        leadQuality: null,
        loadQuality: null,
        overallSize: null,
        passengersNum: null,
        prepareQuality: null,
        record: null,
        remarks: null,
        axleCount: null,
        loadCapacity: null,
        isDelete: null,
        status: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {

      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询车辆信息列表 */
    getList() {
      this.loading = true;
      listCar(this.queryParams).then(response => {
        this.carList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        carId: null,
        companyId: null,
        plateNum: null,
        model: null,
        picture: null,
        Permit: null,
        permitB: null,
        owner: null,
        vin: null,
        addr: null,
        vehicleType: null,
        useCharacter: null,
        engineNum: null,
        officialSeal: null,
        issueDate: null,
        registerDate: null,
        fileNumber: null,
        totalQuality: null,
        leadQuality: null,
        loadQuality: null,
        overallSize: null,
        passengersNum: null,
        prepareQuality: null,
        record: null,
        remarks: null,
        axleCount: null,
        loadCapacity: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        isDelete: null,
        status: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.carId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加车辆信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const carId = row.carId || this.ids
      getCar(carId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改车辆信息";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.carId != null) {
            updateCar(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addCar(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const carIds = row.carId || this.ids;
      this.$modal.confirm('是否确认删除车辆信息编号为"' + carIds + '"的数据项？').then(function() {
        return delCar(carIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('consumer/car/export', {
        ...this.queryParams
      }, `car_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
